local idle_text = {}
local idle_max = 100
for _ = 1, idle_max do
    table.insert(idle_text, class.text:builder {
        w = 250,
		h = 32,
        text = '',
        color = 0xffffffff,
        shadow_offset_x = 4,
        shadow_offset_y = -4,
        shadow = 0xff000000,
        show_flag = true,
		font_size = 14,
        icon = {
            type = 'panel',
            x = -32,
            y = 0,
            w = 32,
            h = 32,
        }
    })
end


local texttag = {}
ac.ui_tag = texttag
setmetatable(texttag, texttag)
texttag.group = {}

-- 结构
local mt = {}
texttag.__index = mt

-- 类型
mt.type = 'ui tag'

-- 句柄
mt.handle = nil

-- 文本内容
mt.string = '无文本'

-- 缩放
mt.scale = 1

-- 初始位置
mt.position = ac.point(0, 0)

-- 速度
mt.speed = 0

-- 角度
mt.angle = 90

-- 颜色
mt.color = 0xffffffff
mt.alpha = 1

mt.clock = 0

-- 生命周期
mt.life = 3

-- 淡化
mt.fade = 2

-- 可见性
mt.show = true

-- 设置文本
function mt:set_text(string)
	if string then
		self.string = string
    end

    if self.handle then
        self.handle:set_text(string or self.string)
    end
end

-- 设置大小
function mt:set_scale(scale)
	if scale then
		self.scale = scale
    end
	self.handle:set_size(scale or self.scale)
end

function mt:add_scale(diff)
	self.scale = self.scale + diff
	self.handle:set_size(self.scale)
end

-- 设置位置
mt.offset_x = 0
mt.offset_y = 0
function mt:set_position(position)
	if not self.show then
		return
	end
	if position then
		self.position = position
	end
	local point = self.position
	local x, y, z = point:get()
	
    local sx, sy = ac.frame.w2s(x, y, z + point:get_height())

    if sx == nil or sx < 0 or sy < 32 or sx > 1920 or sy > 768 then
        self.handle:hide()
    else
        japi.FrameSetAbsolutePoint(self.handle.handle, 0, (sx + self.offset_x) / 1920 * 0.8,
            (1080 - (sy + self.offset_y)) / 1080 * 0.6 + japi.FrameGetTextHeight(self.handle.handle))
        if self.player then
            if self.player:is_self() then
                self.handle:show()
            else
                self.handle:hide()
            end
        else
            self.handle:show()
        end
    end
end

-- 设置颜色
function mt:set_color(color)
    self.handle:set_color(color or self.color)
end

-- 透明度
function mt:set_alpha(alpha)
	self.handle:set_alpha(alpha or self.alpha)
end

-- 创建漂浮文字
function texttag:__call(texttag)
    setmetatable(texttag, self)
    
    local handle = table.remove(idle_text,#idle_text)
    if not handle then
        texttag.removed = true
        return texttag
	end
	
	local level = idle_max - #idle_text
    handle:set_level(level)
    handle.icon:set_normal_image(texttag.icon or '')

    texttag.handle = handle
	texttag.handle:set_alpha(1)
    handle:set_level(idle_max - #idle_text)
	texttag:set_text()
	texttag:set_scale()
	texttag:set_position()
	texttag:set_color()
    
	
    self.group[texttag] = true
	return texttag
end

-- 移除漂浮文字
function mt:remove()
	if self.removed then
		return
    end
    
	self.removed = true
    
	self.handle:hide()
	self.handle:unbind_world()
	self:set_scale(1)
	
	table.insert(idle_text, self.handle)
	
	texttag.group[self] = nil
	
    --重新对图层排序
	local level = self.handle.level
    for tt in pairs(texttag.group) do
        if tt.handle.level>level then
            tt.handle:set_level(tt.handle.level - 1)
        end
    end
	
end

-- 最大弹跳
mt.jump_max = 1
mt.jump_min = 0.5
-- 弹跳时间
mt.jump = 0
mt.x_speed = 10

function mt:move(delta)
	self.clock = self.clock + delta
	if self.clock > self.life - self.fade then
		if self.clock > self.life  then
			self:remove()
			return
		else
			self.alpha = math.max(self.alpha - self.fade * delta,0)
			self:set_alpha()
		end
	end
    local x,y,z = self.position:get()
	if self.jump > 0 then
		if self.clock > self.jump then
			y = y - self.speed / (delta * 1000) / 10
		else
			y = y + self.speed / (delta * 1000)
		end
		local value = self.jump_max * math.sin(math.max(self.clock,0) / self.life * 180) + self.jump_min
        self:set_scale(value)
		x = x + self.x_speed / (delta * 1000)
	else
		x = x + self.speed * math.cos(self.angle)*delta
		y = y + self.speed * math.sin(self.angle)*delta
        self:set_scale()
	end
	self.position = ac.point(x,y)
	
	self:set_position()
end


ac.hardware:on("界面绘制")(function(_,delta)
    for tt in pairs(texttag.group) do
        tt:move(delta)
    end
end)

return texttag

